//API 操作
const API = require('../../../datas/common.js');
let app = getApp();

//创建精选页面对象
Page({
    data: {
        noMoreData: false,
        seeMore: false,
        loadMore: false,
        detail: {},
        comments: [],
        page: 1,
        pageSize: 10,
        commentValue: '',
        commentMinLength: 5,
        commentMaxLength: 100,
        canSend: false
    },
    onLoad: function (options) {
        let that = this;
        let id = options.id;
        let type = options.type;
        API.showLoading('加载中...')
        if (type == 'NEARBY') {
            API.getLastLocation().then((res) => {
                that.getNearbyContent(id, res.longitude, res.latitude);
            }).catch((res) => {
                that.getContentListError(res.message);
            });
        } else {
            that.getNearbyContent(id);
        }
    },
    getNearbyContent: function (id, longitude, latitude) {
        let that = this;
        API.get('/content/nearby/' + id, {
            longitude: longitude ? longitude : '',
            latitude: latitude ? latitude : '',
            page: that.data.page,
            pageSize: that.data.pageSize
        }).then(res => {
            let detail = res.data.content;
            let comments = res.data.comments;
            let hasNext = res.data.hasNext;
            that.setData({
                noMoreData: !hasNext,
                seeMore: hasNext,
                detail: detail,
                comments: comments
            })
            API.hideLoading();
        }).catch((res) => {
            API.showToast(res.message);
        });
    },
    getComment: function (e) {
        let that = this;
        that.setData({
           canSend: e.detail.value.length >= that.data.commentMinLength
        })
    },
    comment: function (e) {
        let that = this;
        API.getUserInfo().then(res => {
            API.showLoading('发表中...')
            let param = {
                openId: res.openId,
                authorName: res.nickname,
                comment: e.detail.value.comment,
                contentId: that.data.detail.id
            }
            API.post('/message/comment', param).then(res => {
                let data = res.data;
                that.setData({
                    comments: [data].concat(that.data.comments),
                    ['detail.commentTimes']: that.data.detail.commentTimes + 1,
                    commentValue: '',
                    canSend: false
                });
                API.hideLoading();
                let comment = {
                    contentId: data.contentId,
                    date: data.created.substring(11, 16),
                    commentUserName: data.authorName,
                    message: data.comment
                };
                app.setCommentData(comment);
            }).catch((res) => {
                API.showToast(res.message);
            });
        }).catch((res) => {
            API.showToast(res.message);
        })
    },
    up: function (event) {
        let that = this;
        API.getUserInfo().then(res => {
            let id = event.currentTarget.dataset.id;
            API.put('/content/up/' + id).then(res => {
                that.setData({
                    ['detail.upTimes']: that.data.detail.upTimes + 1,
                    ['detail.isUp']: true,
                })
            }).catch((res) => {
                API.showToast(res.message);
            });
        }).catch((res) => {
            API.showToast(res.message);
        })
    },
    previewImage: function (event) {
        let current = event.currentTarget.dataset.current;
        let images = this.data.detail.images;
        wx.previewImage({
            current: images[current], // 当前显示图片的http链接
            urls: images
        });
    },
    onReachBottom: function () {
        let that = this;
        if (!that.data.noMoreData) {
            that.setData({
                seeMore: false,
                loadMore: true
            });
            that.getCommentsByPage();
        }
    },
    getCommentsByPage: function () {
        let that = this;
        API.get('/content/nearby/comments/' + that.data.detail.id, {
            page: that.data.page,
            pageSize: that.data.pageSize,
            lastId: that.data.comments[that.data.comments.length - 1].id
        }).then(res => {
            let comments = res.data.comments;
            let hasNext = res.data.hasNext;
            setTimeout(function () {
                that.setData({
                    noMoreData: !hasNext,
                    seeMore: hasNext,
                    loadMore: false,
                    comments: that.data.comments.concat(comments),
                })
            }, 1000)
        }).catch((res) => {
            API.showToast(res.message);
        });
    }
});